'use strict';

const { Controller } = require('egg');

class DiscoveryController extends Controller {
  async tuijian() {
    const { ctx } = this;
    let type = ctx.request.query.type
    console.log(type,'类型')
    // 画板中的图片的描述、地址、属于哪个画板
    const results = await this.app.mysql.query(`select * from b_img where bid in(select bid from draw_board where btype like '${type}')`)

    // 用户名、头像、采集数、喜欢

    ctx.body = {
        code: 1,
        data:results
    };
  }
  async detail() {
    const { ctx } = this;
    // let pid = ctx.request.query.pid
    // let {pid,uid} = ctx.request.query
    // uid原本是传过来的死数据，现在后端在前端的携带的数据包中获取解密
    let {pid} = ctx.request.query
    let uid=this.ctx.session.uid//取出前端数据包中的缓存加密字符串 然后解密res2[0].uid
    
    console.log(pid)
    const re1 = await this.app.mysql.query(`select * from good where pid=${pid} and uid=${uid}`)
    let islike = null
    if(re1.length>0){
      // 已经喜欢了
      islike=true
    }else{
      islike=false
    }
    // 根据图片id去查相关信息，图片地址，图片描述，属于画板，作者信息
    const results = await this.app.mysql.query(`select * from b_img where pid=${pid}`)
    console.log(results)
    const re2 = await this.app.mysql.query(`select * from draw_board where bid=${results[0].bid}`)
    const re3 = await this.app.mysql.query(`select uid,nickname,touxiang from user where uid=${re2[0].uid}`)
    console.log(re2,'2222')
    console.log(re3,'3333')
    // 还要返回改画板中的所有图片信息
    const re4 = await this.app.mysql.query(`select * from b_img where bid=${results[0].bid}`)
    console.log(re4,'4444')
    ctx.body = {
        code: 1,
        data:[...results,...re2,...re3,[...re4],islike]
    };
  }
  async good() {
    const { ctx } = this;
    // let {pid,uid,good} = ctx.request.query
    let {pid,good} = ctx.request.query
    console.log(pid,'图片id',good)
    let uid=this.ctx.session.uid
    if(good == 'true'){
      // 要点赞
      const results = await this.app.mysql.query(`insert into good values(${pid},${uid})`)
      console.log('已点赞')
      ctx.body = {
        code: 1,
        data:'已点赞'
      };
    }else{
      // 喜欢之后点击就是取消，应该在good表中删除该条喜欢的数据
      const results = await this.app.mysql.query(`delete from good where pid=${pid} and uid=${uid}`)
      console.log('取消点赞')
      ctx.body = {
        code: -1,
        data:'取消点赞'
      };
    }
  }
  async drawborder() {
    const { ctx } = this;
    // let uid = ctx.request.query.uid
    let uid=this.ctx.session.uid
    console.log(uid,'类型')
    const results = await this.app.mysql.query(`select * from draw_board where uid=${uid}`)

    ctx.body = {
        code: 1,
        data:results
    };
  }
  async gather_to() {
    const { ctx } = this;
    let {pid,bid} = ctx.request.query
    console.log(ctx.request.query,'类型')
    // 先查询这张图片的信息
    const results = await this.app.mysql.query(`select * from b_img where pid=${pid}`)
    let {ptext,img} = results[0]
    console.log(ptext,img)
    // 将信息插入到画板图片表——b_img，
    const re2 = await this.app.mysql.query(`insert into b_img(ptext,img,bid) values('${ptext}','${img}',${bid})`)
    

    ctx.body = {
        code: 1,
        data:'采集成功'
    };
  }

}

module.exports = DiscoveryController;
